#include "Player.h"
#include "../SGD Wrappers/CSGD_DirectInput.h"
#include "ObjectFactory.h"
#include "Elemental.h"

CPlayer::CPlayer(void)
{
	m_vQueuedSouls.push_back(0);
	m_vQueuedSouls.push_back(0);
	m_vQueuedSouls.push_back(0);
	m_vQueuedSouls.push_back(0);

	m_vSoulsOwned.push_back(2);
	m_vSoulsOwned.push_back(3);
	m_vSoulsOwned.push_back(4);
	m_vSoulsOwned.push_back(5);
	m_vSoulsOwned.push_back(0);

	m_vUnitsInALanes.push_back(0);
	m_vUnitsInALanes.push_back(0);

	timerCountdown = 0;
	soulTimer = 4;

	m_bUsedFree = false;

	m_pDI	= CSGD_DirectInput::GetInstance();
	m_pOF = CObjectFactory::GetInstance();
	
}


CPlayer::~CPlayer(void)
{
}

void CPlayer::Update(float fElapsedTime)
{
	timerCountdown = timerCountdown + fElapsedTime;

	if( timerCountdown > soulTimer )
	{
		m_vSoulsOwned[FREE]++;
		timerCountdown = 0;
	}
}


//Come back after combat and soul returning is working
//To dequeue make a vector of the order they were made in
//When backspace is pressed pop off the back of the new vector
//Then minus that type in the queued vector
void CPlayer::Input()
{

	if( IsPlayer1 == true )
	{
	if( m_pDI->KeyPressed( DIK_Q ))
	{
		if( m_vSoulsOwned[FIRE] > 0 )
		{
		m_vQueuedSouls[FIRE]++;
		m_vSoulsOwned[FIRE]--;
		m_vCommandLine.push_back(FIRE);
		}
		else if( m_vSoulsOwned[FREE] > 0 && m_bUsedFree == false)
		{
		m_vQueuedSouls[FIRE]++;
		m_vSoulsOwned[FREE]--;
		m_vCommandLine.push_back(FIRE);
		m_bUsedFree = true;
		}
	}

	if( m_pDI->KeyPressed( DIK_W ))
	{
		//water
		if( m_vSoulsOwned[AIR] > 0 )
		{
		m_vQueuedSouls[AIR]++;
		m_vSoulsOwned[AIR]--;
		m_vCommandLine.push_back(AIR);
		}
		else if( m_vSoulsOwned[FREE] > 0 && m_bUsedFree == false)
		{
		m_vQueuedSouls[AIR]++;
		m_vSoulsOwned[FREE]--;
		m_vCommandLine.push_back(AIR);
		m_bUsedFree = true;
		}
	}

	if( m_pDI->KeyPressed( DIK_E ))
	{
		//air
		if( m_vSoulsOwned[WATER] > 0 )
		{
		m_vQueuedSouls[WATER]++;
		m_vSoulsOwned[WATER]--;
		m_vCommandLine.push_back(WATER);
		}
		else if( m_vSoulsOwned[FREE] > 0 && m_bUsedFree == false)
		{
		m_vQueuedSouls[WATER]++;
		m_vSoulsOwned[FREE]--;
		m_vCommandLine.push_back(WATER);
		m_bUsedFree = true;
		}
	}

	if( m_pDI->KeyPressed( DIK_R ))
	{
		//earth
		if( m_vSoulsOwned[EARTH] > 0 )
		{
		m_vQueuedSouls[EARTH]++;
		m_vSoulsOwned[EARTH]--;
		m_vCommandLine.push_back(EARTH);
		}
		else if( m_vSoulsOwned[FREE] > 0 && m_bUsedFree == false)
		{
		m_vQueuedSouls[EARTH]++;
		m_vSoulsOwned[FREE]--;
		m_vCommandLine.push_back(EARTH);
		m_bUsedFree = true;
		}
	}

	//Analize queueSouls vector and spawn 
	
	if( m_pDI->KeyPressed( DIK_1 ))
		{
			if( IsPlayer1 == true )
			{
				for( unsigned int i = 0; i < m_vQueuedSouls.size(); i++ )
					if( m_vQueuedSouls[i] > 0 )
					{
						m_pOF->SpawnElemental(this, 0);

					//Clear after doing everything to it
					for( unsigned int i = 0; i < m_vQueuedSouls.size(); i++ )
						m_vQueuedSouls[i] = 0;
					}
		m_bUsedFree = false;
			}
		}

	if( m_pDI->KeyPressed( DIK_2 ))
		{
			if( IsPlayer1 == true )
			{
				for( unsigned int i = 0; i < m_vQueuedSouls.size(); i++ )
					if( m_vQueuedSouls[i] > 0 )
					{
						m_pOF->SpawnElemental(this, 2);
					//Clear after doing everything to it
					for( unsigned int i = 0; i < m_vQueuedSouls.size(); i++ )
						m_vQueuedSouls[i] = 0;
					}
			}
		m_bUsedFree = false;
		}

	if( m_pDI->KeyPressed( DIK_3 ))
		{
			if( IsPlayer1 == true )
			{
				for( unsigned int i = 0; i < m_vQueuedSouls.size(); i++ )
					if( m_vQueuedSouls[i] > 0 )
					{
						m_pOF->SpawnElemental(this, 1);
					//Clear after doing everything to it
					for( unsigned int i = 0; i < m_vQueuedSouls.size(); i++ )
						m_vQueuedSouls[i] = 0;
					}
			}
		m_bUsedFree = false;
		}
	}

	
	if( m_pDI->KeyPressed( DIK_BACKSPACE ))
		{
			short temp = m_vCommandLine.back();
			m_vQueuedSouls[temp]--;
			if( m_bUsedFree == true )
			{
				m_vSoulsOwned[FREE]++;
				m_bUsedFree = false;
			}
			else
				m_vSoulsOwned[temp]++;
			m_vCommandLine.pop_back();
		}

	if( IsPlayer1 == false )
			{
				if( m_vSoulsOwned[FREE] > 0 )
					{
						m_vQueuedSouls[rand()%4]++;
						m_vSoulsOwned[FREE]--;
					}
				for( unsigned int i = 0; i < m_vQueuedSouls.size(); i++ )
					if( m_vQueuedSouls[i] > 0 )
					{
					m_pOF->SpawnElemental(this,rand()%1);
					//Clear after doing everything to it
					for( unsigned int i = 0; i < m_vQueuedSouls.size(); i++ )
						m_vQueuedSouls[i] = 0;
					}
			}
}

void CPlayer::IncreasePool(vector<short> elementalSouls)
{
	m_vSoulsOwned[FIRE] = m_vSoulsOwned[FIRE] + elementalSouls[FIRE];
	m_vSoulsOwned[WATER] = m_vSoulsOwned[WATER] + elementalSouls[WATER];
	m_vSoulsOwned[AIR] = m_vSoulsOwned[AIR] + elementalSouls[AIR];
	m_vSoulsOwned[EARTH] = m_vSoulsOwned[EARTH] + elementalSouls[EARTH];
}

void CPlayer::PoolGeneration( short elementalType )
{
	m_vSoulsOwned[elementalType]++;
}
